package com.liu.student.controller;

import com.liu.student.entity.Enrollment;
import com.liu.student.service.EnrollmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 选课API控制器
 */
@RestController
@RequestMapping("/api/enrollments")
public class EnrollmentController {
    
    @Autowired
    private EnrollmentService enrollmentService;

    @PostMapping("/select")
    public String select(@RequestParam Long studentId, @RequestParam Long courseId) {
        try {
            enrollmentService.enrollCourse(studentId, courseId);
            return "选课成功";
        } catch (Exception e) {
            return "选课失败: " + e.getMessage();
        }
    }

    @PostMapping("/drop")
    public String drop(@RequestParam Long studentId, @RequestParam Long courseId) {
        try {
            enrollmentService.dropCourse(studentId, courseId);
            return "退课成功";
        } catch (Exception e) {
            return "退课失败: " + e.getMessage();
        }
    }

    @GetMapping("/my")
    public List<Enrollment> my(@RequestParam Long studentId) {
        return enrollmentService.findByStudentId(studentId);
    }
}